package com.example.knowledge.fragement;

import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.androidkun.xtablayout.XTabLayout;
import com.example.baselib.base.BaseMVPFragment;
import com.example.baselib.baseurl.GuidUrl;
import com.example.knowledge.adapter.KnoeledgeAdapter;
import com.example.knowledge.presenter.KnowLedgePresenter;
import com.example.news.R;

import java.util.ArrayList;
import java.util.List;


public class KnowLedgeFragment extends BaseMVPFragment<KnowLedgePresenter> implements ViewPager.OnPageChangeListener {

    private XTabLayout tablayout;
    private ViewPager viewpage;
    private List<Fragment> mFragments = new ArrayList<>();//标签页集合
    private KnoeledgeAdapter knoeledgeAdapter;//自定义适配器
    private List<String> mTitles = new ArrayList<>();//标签集合

    private ConsultFragment consultFragment;
    private CourseFragment courseFragment;

    @Override
    protected KnowLedgePresenter createPresenter() {
        return new KnowLedgePresenter();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_knoledge;
    }

    @Override
    protected void initView(View rootView) {
        tablayout = rootView.findViewById(R.id.xtablayout);
        viewpage = rootView.findViewById(R.id.viewpage);
        initTablayout();
    }

    private void initTablayout() {
        if (mTitles.size() <= 0) {
            mTitles.add("资讯");
            mTitles.add("课程");
        }

        if (consultFragment == null) {
            consultFragment = (ConsultFragment) ARouter.getInstance().build(GuidUrl.Consult_Fragment).navigation();//data.getData().getPerson().getName()

        }
        if (!mFragments.contains(consultFragment)) {
            mFragments.add(consultFragment);
        }

        if (courseFragment == null) {
            courseFragment = (CourseFragment) ARouter.getInstance().build(GuidUrl.Course_Fragment).navigation();
        }

        if (!mFragments.contains(courseFragment)) {
            mFragments.add(courseFragment);
        }
        if (knoeledgeAdapter == null) {
            knoeledgeAdapter = new KnoeledgeAdapter(getActivity().getSupportFragmentManager(), mFragments, mTitles);
        }
        //ViewPager设置适配器
        viewpage.setAdapter(knoeledgeAdapter);
        //ViewPager设置适配器
        viewpage.addOnPageChangeListener(this);
//        if ("1".equals(currentPoi)) {
//            viewpage.setCurrentItem(1);
//        }
        //viewpage.setCurrentItem(1);
        //设置ＴａｂＬａｙｏｕｔ模式，当前为系统默认模式．
        //tablayout.setTabMode(TabLayout.MODE_FIXED);
        //关联viewpager 和 tablayout
        tablayout.setupWithViewPager(viewpage);
        //添加适配器
        tablayout.setTabsFromPagerAdapter(knoeledgeAdapter);
        //TabLayoutIndicatorUtils.setTabLayoutIndicator(tablayout, 100, 70);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
